#include<stdio.h>
#include<stdlib.h>
#include<string.h>

// @ 结构体嵌套一级指针

struct Person{
    char * name;
    int age;
};


struct Person ** allocateSpace(){
   struct Person **temp =  malloc(sizeof(struct Person*)*3);

    for(int i=0;i<3;i++){
        // 创建结构体内存
        temp[i] = malloc(sizeof(struct Person));
        temp[i]->name = malloc(sizeof(char )*64);
        // 给姓名赋值
        sprintf(temp[i]->name,"name_%d",i+1);
        temp[i]->age  = 20+i;
    }

   return temp;
}

void printfArr(struct Person **p,int len){
    for(int i=0;i<len;i++){
        printf("姓名： %s , 年龄: %d\n", p[i]->name,p[i]->age);
    }
}

void freeSpace(struct Person **p, int len){
    if(p == NULL){
        return ;
    }

    if(len <=0){
        return;
    }
    for(int i=0;i<len;i++){
        
        if(p[i]->name !=NULL){
            free(p[i]->name);
            p[i]->name = NULL;
        }

        free(p[i]);
        p[i] = NULL;
    }

      free(p);
      p=NULL;
}

void test(){
    // struct Person ** p = malloc(sizeof(struct Person *) *3);
    struct Person ** p = NULL;
    p = allocateSpace();

    // 打印数组
    printfArr(p,3);

    // 释放
    freeSpace(p,3);
    p=NULL;

}


void main(){

    test();
    
}